package com.oyjd;

import java.io.File;
import java.text.MessageFormat;
import java.util.List;


public class DownPic {
	public static String url1 = "http://wp.hiapk.com/";
	public static String reg1 = "<li>.*?<h3><a href=\".*?\" target=\"_blank\">(.*?)</a></h3>.*?<a href=\"(.*?)\" class=\"btn14\" target=\"hiddenIFrame\".*?>.*?</li>";

	public static String reg2 = "'(.*?)'";

	public static String sql = "insert into T_PICTURE (PIC_ID, DESCRIPTION, FORMAT, NAME, SOU_PATH, SPACE_SIZE, CREATOR, CREATE_TIME, LAST_EDITOR, LAST_UPDATE_TIME, STATUS) values (''{0}'', ''{1}'', ''{2}'', ''{1}'', ''{3}'', {4}, ''admin'', now(), ''admin'', now(), 0);";
	public static String hot_sql = "insert into T_HOT_SOURCE (DOWN_COUNT, SOU_ID, SOU_TYPE, ID) values ({0}, ''{1}'', ''{2}'', ''{3}'');";
	public static String order_sql = "insert into T_PICTURE_TYPE_ORDER (ORDER_KEY, PIC_ID, TYPE, ID) values ({0}, ''{1}'', ''{2}'', ''{3}'');";

	public static String souPath = "E:/SL/trunk/pic/";
	public static String sqlPath = "E:/SL/trunk/script/pic.sql";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		HttpNet.setRequestProperty("Referer", url1);
		HttpNet hn = new HttpNet();
		String c = new String(hn.getBytesFromNet(url1));
		List<String[]> tmp1 = RegexUtil.getComplex(c, reg1, new int[] { 1, 2 });
		int count = tmp1.size();
		StringBuffer sb = new StringBuffer("use sldb;\r\n\r\n");
		StringBuffer sb1 = new StringBuffer("\r\n#==============================\r\n");
		StringBuffer sb2 = new StringBuffer("\r\n#==============================\r\n");
		for (int i = 0; i < tmp1.size(); i++) {
			String name = tmp1.get(i)[0];
			String link = tmp1.get(i)[1];
			String picLink = RegexUtil.getSingle(new String(hn.getBytesFromNet(link)), reg2);
			String ex = picLink.substring(picLink.lastIndexOf(".") + 1);
			System.out.println((i + 1) + "--->" + name + "---" + link + "---" + picLink);
			// down
			String newName = FileOP.getNewHashName(picLink, "sl_pic");
			File file = new File(souPath + newName);
			if (file.exists() && file.length() == 0) {
				file.delete();
			}
			if (!file.exists()) {
				hn.downFile(picLink, null, souPath + newName);
			}

			// sql
			if (file.exists() && file.length() > 0) {
				String souId = FileOP.getUUID();
				String newSql = MessageFormat.format(sql, souId, name, ex, newName, Long.toString(file.length()));
				sb.append(newSql).append("\r\n");
				String newHotSql = MessageFormat.format(hot_sql, count--, souId, "TYPE_PIC", FileOP.getUUID());
				sb1.append(newHotSql).append("\r\n");
				String typeSql = MessageFormat.format(order_sql, i + 1, souId, "Pic_Boutique", FileOP.getUUID());
				sb2.append(typeSql).append("\r\n");
			}

		}
		FileOP.writeFile(sqlPath, sb.toString() + sb1.toString() + sb2.toString(), "gb2312");

	}

}
